package mrpheu.demo.client.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

import mrpheu.demo.basic.interfaces.IMrPheuOPC;


public class ChatUDPClientThread implements Runnable, IMrPheuOPC {
    private DatagramPacket packetServer;
    private DatagramSocket socket;
    
    public ChatUDPClientThread(DatagramSocket socket, DatagramPacket packetServer) {
        this.socket = socket;
        this.packetServer = packetServer;
    }
    
    @Override
    public boolean open() {
        return true;
    }

    @Override
    public void process() {
        while(!ChatUDPClient.off.get()) {
            System.out.println("Waiting tu server...");
            if(UDPIOImpl.receiveData(socket) == null) {
                return;
            }
            byte[] buf = new byte[512];
            DatagramPacket packetAck = new DatagramPacket(buf, buf.length, this.packetServer.getAddress(), this.packetServer.getPort());
            System.out.println("Send ack...");
            //UDPIOImpl.sendData(this.socket, packetAck);
        }
        this.close();
    }

    @Override
    public boolean close() {
        if(this.socket != null) {
            this.socket.close();
        }
        return true;
    }

    @Override
    public void run() {
        this.open();
        this.process();
        //this.close();
        
    }

}
